<?php
namespace app\index\controller;
use app\index\model\Users;
use app\index\model\Order;
use think\Controller;
use think\DB;
date_default_timezone_set('PRC');
class Orders extends Controller{
    public function index(){ 
        if(request() -> isPost()){
            $data = model('Orders')->index();
            if($data['code'] == 1){
              $this->success($data['msg'],'index/index/index','',1); 
            }else{
              $this->error($data['msg'],'index/index/index','',1); 
            }
        }else{
            //请求失败
           $this->error("请求失败",'index/index/index','',1); 
        }
        
    }
    
    public function wins(){
        //赔率的设置
        $result = db('result')->field('id,qishu,ball1,status')->where('open',0)->order('id desc')->select();
        foreach ($result as $key => $value) {
            if(!empty($value['ball1'])){
               $win = $this->win($value['qishu']);
               return $win;
            }
        }
    }

    //用户中奖
    public function win($qishu){
        date_default_timezone_set('PRC');
        //赔率的设置
        $set = db('set')->field('allodds')->select();
        $allodds = $set[0]['allodds'];
        $result = db('result')->field('id,qishu,status')->where('qishu',$qishu)->order('id desc')->find();
        $data['qishu'] = $result['qishu'];
        $data['updown'] = $result['status'];
        $datas['qishu'] = $result['qishu'];
        $datas['updown'] = array('neq',$data['updown']);
        /*$result = db('result')->field('id,qishu,status')->limit(1,1)->order('id desc')->select();
        $data['qishu'] = $result[0]['qishu'];
        $data['updown'] = $result[0]['status'];
        $datas['qishu'] = $result[0]['qishu'];
        $datas['updown'] = array('neq',$data['updown']);*/
        //用户中奖状态更改
        $alls = db('order')
             ->field('oid,uid,money,status')
             ->where($data)
             ->select(); 
        foreach($alls as $key => $value){        
            if($value['status'] == 3){
                 $win[$key]['oid'] = $value['oid'];
                 $win[$key]['uid'] = $value['uid'];
                 $win[$key]['status'] = 1;
                 $win[$key]['winmoney'] = $value['money']*($allodds);
                 $win[$key]['wintime'] = date('Y-m-d H:i:s');
            }
        } 
        //用户中奖更改
        if(!empty($win)){
            $order = new Order;
            $msg['zhong'] = $order->saveAll($win);
            $all = db('order')
                       ->field('uid,SUM(money) as money')
                       ->group('uid')
                       ->where($data)
                       ->select(); 
                   foreach($all as $key => $value){
                    $user = db('users')->field('uid,money')->where('uid',$value['uid'])->select();
                         foreach ($user as $k => $v) {
                               $all[$key]['money']= $v['money']+$allodds*$value['money'];
                         }
                    } 
                    $users = new Users;
                    $msg['users'] = $users->saveAll($all); 
        }
        //用户未中奖状态更改
        $all1 =  db('order')
             ->field('oid,uid,money,status')
             ->where($datas)
             ->select();      
        foreach($all1 as $key => $value){
            if($value['status'] == 3){
                $wins[$key]['oid'] = $value['oid'];
                $wins[$key]['uid'] = $value['uid'];
                $wins[$key]['status'] = 0;
                $wins[$key]['winmoney'] = 0;
                $wins[$key]['wintime'] = date('Y-m-d H:i:s');
           }
        }
        if(!empty($wins)){
            $order = new Order;
            $msg['wei'] = $order->saveAll($wins); 
        }
        if(!$alls && !$all1){
            $open['open'] = 1;
            $qi = db('result')->where('qishu',$qishu)->update($open);
            if($qi){
              return 200;
            }else{
                return 201;
            }
        }
        if(isset($msg)){
            $open['open'] = 1;
            $qi = db('result')->where('qishu',$qishu)->update($open);
            if($qi){
              return 200;
            }else{
                return 201;
            }
        }else{
            return 201;
        }
     }
}

